<?php
/*
# ------------------------------------------------------------------------
# JA Job Board Package
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com.
# ------------------------------------------------------------------------
*/  

/**
 * @package Joomla
 * @subpackage JobBoard
 */
class JobBoardModelJAShortList extends JModel
{
    var $_pagination = null;
    var $_table = null;
	var $_task_table= null;
	var $task_id = null;    
    
    function __construct($jbconfig = array()){
        parent::__construct($jbconfig);
        
        $this->set('name', 'shortlist');
        $this->set('fullname', JBPREFIX.'shortlist');
    }
    
    
//  PhuongLH - Save jobs - End -
     
    function remove($task = 'remove'){
        $cid = JRequest::getVar('cid',array(), 'post', 'array');
        $n =  count($cid);
        JArrayHelper::toInteger( $cid );
        $user = &JFactory::getUser();
		
        if($n && $user->id){
            if($task == 'remove'){
                $query = "DELETE FROM #__ja_shortlist WHERE user_id = ". (int)$user->id ." AND item_id = ". implode(" OR item_id = ", $cid);
                echo $query;
                $db = &JFactory::getDBO();
                $db->setQuery($query);
                if($db->query()){
                    return $n;
                }
            }
        }
        
        return 0;
    }
    
    function starred($task = 'starred'){
        global $mainframe;
        
        $cid = JRequest::getVar('cid');
        $cid = $cid[0];
        $is_starred = $task == 'starred' ? 1 : 0;
        $db = &JFactory::getDBO();
		
        // Check
        $user = &JFactory::getUser();
        $table_name = JRequest::getVar('table_name', 'ja_jobs');

        // Select id that already in the table
        if($cid){
            if($is_starred){
            	
            	$sql = "select id from #__ja_shortlist where user_id=".(int)$user->id." and item_id=$cid and listtype='$table_name'";
            	$db->setQuery($sql);
            	
            	$short_id = $db->loadResult();
            	
				$shortlisttb = &$this->getTable();               
				// Insert
				if($short_id)	
					return 'done';
				$shortlisttb->id = $short_id;
				$shortlisttb->item_id = $cid;
				$shortlisttb->user_id = $user->id;
				$shortlisttb->listtype = $table_name;
				$shortlisttb->save_date = date("Y-m-d H:i:s");
				
				$result =  $shortlisttb->store();
				if ($result)
				return 'new';

			} else {
			    $query = "DELETE FROM #__ja_shortlist "
					." WHERE user_id = ". (int)$user->id 
					." AND listtype='$table_name' AND item_id = ". (int)$cid ;
			}
			$db->setQuery($query);
            return $db->query();
		}
        return false;
    }
    
    function addNote(){
        global $mainframe;
        
        $db = &JFactory::getDBO();
		
        // Check
        $user = &JFactory::getUser();
        $item = $this->getItemForAddNote();        

        // Select id that already in the table
        $item->bind(JRequest::get('request'));
        
		$result = $item->store();				
		
        return $result;
    }
    /**
    * Get shortlist table instance
    * @return JobBoardTableJA_ShortList Instance of shortlist table
    */
    function getTable(){
       if($this->_table == null){
           $this->_table = &JTable::getInstance('ja_shortlist', 'JobBoardTable');
       }
       return $this->_table;
    }
    
    
    /**
    * Get page navigator object
    * @return 
    */
    /*
    function &getPagination(){
        if ($this->_pagination == null) {
            $this->getItems();
        }
        return $this->_pagination;
    }*/
    
    /*function getItems(){
        global $mainframe;
        
        $user = &JFactory::getUser();
        
        if($user->id){
            $limit                = $mainframe->getUserStateFromRequest( 'global.list.limit',                            'limit',            $mainframe->getCfg( 'list_limit' ),    'int' );
            $limitstart            = $mainframe->getUserStateFromRequest( JBCOMNAME.'.shortlist.limitstart',        'limitstart',        0,                'int' );
            
            $db = &JFactory::getDBO();

            $db->setQuery($this->_buildListQuery());
            
            $items = $db->loadObjectList();
            
            $total = count( $items );

            jimport('joomla.html.pagination');
            $this->_pagination = new JPagination( $total, $limitstart, $limit );
            
            // slice out elements based on limits
            $list = array_slice( is_array($items) ? $items : array(), $this->_pagination->limitstart, $this->_pagination->limit );
            
            return $list;
        }
        
        return array();        
    }*/
    
    /*function _buildListQuery(){
        global $mainframe;
        
        $user = &JFactory::getUser();
        $table_name = $mainframe->getUserStateFromRequest( JBCOMNAME .".shortlist.table_name", 'listtype', 'ja_jobs', 'cmd' );
        
        // $table_name must have a field named 'title'
        $query = "";
        if($table_name == "ja_jobs"){
            $query = "SELECT sl.id, sl.item_id, a.title as title, b.id AS profile_id,  b.name as company, sl.listtype, sl.save_date, sl.is_starred"
            ." FROM #__ja_shortlist AS sl "
            ."      LEFT JOIN #__$table_name AS a ON sl.item_id = a.id"
            ."      LEFT JOIN #__ja_profiles AS b ON a.profile_id = b.id"
            ;
        } else if($table_name == "ja_applications"){
             $query = "SELECT sl.id, sl.item_id, a.subject as title, sl.listtype, sl.save_date, sl.is_starred"
            ." FROM #__ja_shortlist AS sl "
            ."      LEFT JOIN #__$table_name AS a ON sl.item_id = a.id"
            ;
        } else if($table_name == "ja_resumes"){
             $query = "SELECT sl.id, sl.item_id, a.title as title, a.email, sl.listtype, sl.save_date, sl.is_starred"
            ." FROM #__ja_shortlist AS sl "
            ."      LEFT JOIN #__$table_name AS a ON sl.item_id = a.id"
            ;
        }
        
        $where = " WHERE sl.user_id = ". $user->id ." AND listtype='". $table_name ."'";
        $query = empty($query)?"": $query . $where;

        return $query;
    }
    
     /**
    * Remove field record
    * @return Number of removed records
    */
     /*
//  PhuongLH - Save jobs - Start -   
	function savejob($task = 'savejob')
	{
		
		$user = &JFactory::getUser();
		if ($user->id)
		{
			$db = &JFactory::getDBO();
			$query = 'INSERT INTO #__ja_shortlist ' 
			.'(user_id, item_id, listtype, save_date) 
			 VALUES ('
			.$user->id.','
			.$this->_task_id.',"'
			.$this->_task_table.'","'
			.date('Y-m-d h:i:s').'")'
			;
			$db->setQuery($query);
			if($db->query())
			{
				return true;
			}
			else 
				return false;
		}
	}*/
     
     function getItemForAddNote()
     {
     	$user = JFactory::getUser();
     	$db = JFactory::getDBO();
     	$item_id = JRequest::getVar('item_id');
     	$listtype = JRequest::getVar('listtype');
     	$query = "SELECT * FROM #__ja_shortlist WHERE item_id=".(int)$item_id." and user_id='$user->id' and listtype='$listtype'";
     	$db->setQuery($query);
     	$item = $db->loadObject();
     	if (!$item)
     		$item = $this->getTable();
     	else 
     	{
     		$table = $this->getTable();
     		$table->load($item->id);
     		$item = $table;
     	}
     	return $item;
     }
     
     
     function multi_starred($task='starred'){
     	global $mainframe;
        $cids = JRequest::getVar('cid');
        $is_starred = $task == 'starred' ? 1 : 0;
        $db = &JFactory::getDBO();
		
        // Check
        $user = &JFactory::getUser();
        $table_name = JRequest::getVar('table_name', 'ja_jobs');

        // Select id that already in the table
        if ($is_starred)
        {
	        for ($i=0;$i<count($cids);$i++)
	        {
	        	$cid = $cids[$i];
	        	$sql = "select id from #__ja_shortlist where user_id=".(int)$user->id." and item_id=".(int)$cid." and listtype='$table_name'";
	        	$db->setQuery($sql);
	        	
	        	$short_id = $db->loadResult();
	        	
				$shortlisttb = &$this->getTable();               
				// Insert
				if($short_id)	
					continue;
				$shortlisttb->id = $short_id;
				$shortlisttb->item_id = $cid;
				$shortlisttb->user_id = $user->id;
				$shortlisttb->listtype = $table_name;
				$shortlisttb->save_date = date("Y-m-d H:i:s");
				
				$result =  $shortlisttb->store();
			}
        }
        else 
        {
        	$str_cid = implode(',',$cids);
        	$sql = "DELETE FROM #__ja_shortlist WHERE user_id=".(int)$user->id." and item_id IN ($str_cid) and listtype='$table_name'";
        	$db->setQuery($sql);
	        $result = $db->query();
	        return $result;
        }
		return true;
        
    }
     
}
?>
